import requests
import json


class Map:
    def __init__(self, api_key):
        self.api_key = api_key
        self.base_url = "https://restapi.amap.com/v3"


    def get_address(self,address,city=None):
        '''

        :param address: 结构化地址信息，
        :param city: 查询城市，
        :return: 返回一个字典，值分别为查询地点的具体位置，与坐标
        '''
        params = {
            'key': self.api_key,
            'address': address,
            'city': city,
        }
        response = requests.get(self.base_url+'/geocode/geo', params=params)
        data = response.json()
        geocodes=data.get('geocodes')
        if geocodes is None or len(geocodes) == 0:
            return '未查询到该地点'
        else:
            formatted_address=geocodes[0]['formatted_address']
            location=geocodes[0].get('location')
            return {'formatted_address': formatted_address,'location':location}

    def driver(self,dict_):
        '''
        dict中含有以下参数
        :param origin: 起点坐标
        :param destination:终点坐标
        :return: 返回列表，内还有一个或多个列表，表示不同的路线，每条路线内有多个字典每个字典内为每段路程的状态，值分别为当前需要保持的状态，下个状态的调整，下个状态调整后的结果
        '''
        try:
            if type(dict_) == dict:
                pass
            else:
                dict_=json.loads(dict_)
            origin=dict_.get('origin')
            destination=dict_.get('destination')
            result = []
            params = {
                'key': self.api_key,
                'origin': origin,
                'destination': destination,
            }
            response = requests.get(self.base_url+'/direction/driving', params=params)
            data = response.json()
            paths=data.get('route').get('paths')
            for path in paths:
                steps=path.get('steps')
                stem=[]
                for step in steps:
                    dic={}
                    dic['instruction']=step.get('instruction')
                    dic['action']=step.get('action')
                    dic['assistant_action']=step.get('assistant_action')
                    stem.append(dic)
                result.append(stem)
            return result
        except Exception as e:
            return '请检查字典输入中的起点与终点是否为坐标'

    def transit(self,dict_):
        '''
                dict中含有以下参数
        :param origin: 起点坐标
        :param destination:终点坐标
        :param city: 城市名
        :return: 返回列表，内还有一个或多个列表，表示不同的路线，每条路线内有多个字典，在这层字典中有两个键，分别为walking与bus前者代表步行，值为一个列表，内有多个字典，每个字典内为每段路程的状态，值分别为当前需要保持的状态，下个状态的调整，下个状态调整后的结果，bus键对应公共交通工具，值为一个列表，内有多个字典值分别为，公共交通工具线路名，公共交通工具类型，该段路程目的地，途中经过的站点
        '''
        try:
            if type(dict_) == dict:
                pass
            else:
                dict_=json.loads(dict_)
            origin=dict_.get('origin')
            destination=dict_.get('destination')
            city=dict_.get('city')
            result = []
            params = {
                'key': self.api_key,
                'origin': origin,
                'destination': destination,
                'city': city,
            }
            response = requests.get(self.base_url+'/direction/transit/integrated', params=params)
            data = response.json()
            transits=data.get('route').get('transits')
            for transit in transits:
                segments=transit.get('segments')
                method=[]
                for segment in segments:
                    seg={}
                    if segment.get('walking')==[]:
                        pass
                    else:
                        walk=[]
                        walking=segment.get('walking')
                        steps=walking.get('steps')
                        for step in steps:
                            stem={}
                            stem['instruction']=step.get('instruction')
                            stem['action']=step.get('action')
                            stem['assistant_action']=step.get('assistant_action')
                            walk.append(stem)
                        seg['walking']=walk
                    if segment.get('bus')==[]:
                        pass
                    else:
                        bus_lis=[]
                        bus=segment.get('bus')
                        buslines=bus.get('buslines')
                        for busline in buslines:
                            btem={}
                            btem['name']=busline.get('name')
                            btem['type']=busline.get('type')
                            btem['arrival_stop_name']=busline.get('arrival_stop').get('name')
                            via_stops=busline.get('via_stops')
                            via_lis=[]
                            for via_stop in via_stops:
                                name=via_stop.get('name')
                                via_lis.append(name)
                            btem['via_stops']=via_lis
                            bus_lis.append(btem)
                        seg['bus']=bus_lis
                    method.append(seg)
                result.append(method)
            return result
        except Exception as e:
            return '请检查字典输入中的起点与终点是否为坐标'


api_key="cf9b7ef249560aa6a48279d8eb8b5b7c"
map=Map(api_key)


